package com.isprint.jslx.httpclient;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.log.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URLDecoder;
/**
 * Created by ydw on 2017/7/14.
 */
public class HttpClient {

    private static Log log=Log.getLog("HttpClient");
    /**
     * 向指定 URL 发送POST方法的请求
     * @param url 发送请求的 URL
     * @param  jsonParam 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @param  noNeedResponse   是否需要返回值
     * @return 所代表远程资源的响应结果
     */
    public static String httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){
        //post请求返回结果
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String result_str = "";
        HttpPost method = new HttpPost(url);
        try {
            if (null != jsonParam) {
                //解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            CloseableHttpResponse result =  httpClient.execute(method);

            url = URLDecoder.decode(url, "UTF-8");
            /**请求发送成功，并得到响应**/
            if (result.getStatusLine().getStatusCode() == 200) {

                try {
                    /**读取服务器返回过来的json字符串数据**/
                    result_str = EntityUtils.toString(result.getEntity());
                    if (noNeedResponse) {
                        return null;
                    }
                    /**把json字符串转换成json对象**/
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("post请求提交失败:" + url, e);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("post请求提交失败:" + url, e);
        }
        return result_str;
    }

    /**
     * 发送get请求的方法
     * @param url 参数已经拼接到url路径上
     * @return
     */
    public static String httpGet(String url){
        //get请求返回结果
        String jsonResult = null;
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = client.execute(request);

            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                jsonResult = EntityUtils.toString(response.getEntity());
            } else {
                log.error("get请求提交失败:" + url);
            }
        } catch (IOException e) {
            log.error("get请求提交失败:" + url, e);
        }
        return jsonResult;
    }
}
